package com.wgtgt.find_text;

import java.io.File;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

public class FindText {

	public static void scan(String path, String keyWord) {
		File folder = new File(path);
		scan(folder, keyWord);
	}

	private static void scan(File folder, String keyWord) {
		if (folder.isFile()) {
			List<String> readUtf8Lines = FileUtil.readUtf8Lines(folder);
			if (CollUtil.isNotEmpty(readUtf8Lines)) {
				for (int i = 0; i < readUtf8Lines.size(); i++) {
					String line = readUtf8Lines.get(i);
					if (StrUtil.isNotBlank(line)) {
						if (line.contains(keyWord)) {
							StringBuilder sb = new StringBuilder();
							sb.append("第");
							sb.append(StrUtil.fillBefore(i + "", ' ', 8));
							sb.append("行, ");
							sb.append("【文本】");
							sb.append(line);
							System.out.println(sb.toString());
						}
					}
				}
			}
		} else {
			File[] listFiles = folder.listFiles();
			for (File file : listFiles) {
				scan(file, keyWord);
			}
		}
	}
}
